﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Things.Bar.Models;

namespace Things.Bar
{
    public class NoticeRepository
    {
        public bool Restore(Notice notice)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                container.AddToNoticeSet(notice);
                if (container.SaveChanges() > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public bool Delete(Notice notice)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                container.NoticeSet.Attach(notice);
                container.ObjectStateManager.ChangeObjectState(notice, System.Data.EntityState.Deleted);
                if (container.SaveChanges() > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public bool Update(Notice notice)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                container.NoticeSet.Attach(notice);
                container.ObjectStateManager.ChangeObjectState(notice, System.Data.EntityState.Modified);
                if (container.SaveChanges() > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public Notice Find(Guid id)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                return container.NoticeSet.Where(p => p.Id == id).FirstOrDefault<Notice>();
            }
        }

        public List<Notice> FindAll()
        {
            using (Models.BarContainer container = new BarContainer())
            {
                return container.NoticeSet.ToList<Notice>();
            }
        }

        public List<Notice> FindAll(Guid typeId)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                return container.NoticeSet.Where(p=>p.TypeId == typeId).ToList<Notice>();
            }
        }

        public bool SaveHotNotice(HotNotice notice)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                if (container.HotNoticeSet.Where(p => p.Id == notice.Id || p.NoticeId == notice.NoticeId)
                    .Select(p => p.Id).FirstOrDefault() != default(Guid))
                {
                    // 更新
                    container.HotNoticeSet.Attach(notice);
                }
                else
                {
                    container.HotNoticeSet.AddObject(notice);
                }

                if (container.SaveChanges() > 0)
                {
                    return true;
                }
            }
            return false;
        }

        public List<HotNotice> FindHotNotices()
        {
            using (Models.BarContainer container = new BarContainer())
            {
                return container.HotNoticeSet.OrderBy(p => p.OrderIndex).ToList();
            }
        }

        public HotNotice FindHotNotice(Guid id)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                return container.HotNoticeSet.Where(p => p.Id == id).FirstOrDefault();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public bool UpHotNotice(Guid id)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                HotNotice notice = container.HotNoticeSet.Where(p => p.Id == id).FirstOrDefault();
                if (notice.OrderIndex <= 0)
                {
                    return true;
                }
                int uOrder = notice.OrderIndex - 1;
                HotNotice uNotice = container.HotNoticeSet.Where(p => p.OrderIndex == uOrder).FirstOrDefault();
                uNotice.OrderIndex = notice.OrderIndex;
                notice.OrderIndex = uOrder;
                if (container.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool DownHotNotice(Guid id)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                HotNotice notice = container.HotNoticeSet.Where(p => p.Id == id).FirstOrDefault();
                if (notice.OrderIndex >=9)
                {
                    return true;
                }
                int dOrder = notice.OrderIndex + 1;
                HotNotice dNotice = container.HotNoticeSet.Where(p => p.OrderIndex == dOrder).FirstOrDefault();
                dNotice.OrderIndex = notice.OrderIndex;
                notice.OrderIndex = dOrder;
                if (container.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }

        public bool DeleteHotNotice(HotNotice notice)
        {
            using (Models.BarContainer container = new BarContainer())
            {
                container.HotNoticeSet.DeleteObject(notice);

                if (container.SaveChanges() > 0)
                {
                    return true;
                }
            }
            return false;
        }
    }
}
